﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Jidong
{
    public partial class 设备登记 : System.Web.UI.Page
    {
        protected string Notice = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            int BillID = 0;
            int fid = 0;
            int bmid = 0;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out BillID);
                }
                if (BillID > 0)
                {
                    this.BuMeng.DataBind();
                    this.SqlDataSource1.SelectParameters["BillID"].DefaultValue = BillID.ToString();
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                else
                {
                    if (Request.QueryString["FID"] != null)
                    {
                        int.TryParse(Request.QueryString["FID"], out fid);
                    }
                    if (Request.QueryString["BMID"] != null)
                    {
                        int.TryParse(Request.QueryString["BMID"], out bmid);
                    }
                    this.BuMeng.Text = bmid.ToString("D6");
                    this.ZiChan.Text = fid.ToString("D8");
                    this.SqlDataSource3.SelectParameters["ID"].DefaultValue = fid.ToString();
                    this.SqlDataSource3.Select(DataSourceSelectArguments.Empty);
                    this.Date1.Text = DateTime.Today.ToLongDateString();

                }
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            this.BianHao.Text = Regex.Replace(this.BianHao.Text, "[^0-9]+","", RegexOptions.None);
            if (this.BianHao.Text.Length != 18)
            {
                this.Notice="“资产编号”录入有误";
                return;
            }
            
            if (this.MingCheng.Text.Trim().Length == 0)
            {
                this.Notice = "“资产名称”必须填写";
                return;
            }
            DateTime d1;
            if (!DateTime.TryParse(this.Date1.Text, out d1))
            {
                this.Notice = "“投产日期”录入有误";
                return;
            }


            double gl;
            this.GongLv.Text=this.GongLv.Text.Trim();
            if (this.GongLv.Text != "")
            {
                if (!double.TryParse(this.GongLv.Text, out gl))
                {
                    this.Notice = "“功率”录入有误";
                    return;
                }
            }

            int zj;
            this.NianXian.Text = this.NianXian.Text.Trim();
            if (this.NianXian.Text != "")
            {
                if (!int.TryParse(this.NianXian.Text, out zj))
                {
                    this.Notice = "“折旧年限”录入有误";
                    return;
                }
            }
            decimal f1, f2;

            if (!decimal.TryParse(this.GouZhiFei.Text, out f1))
            {
                this.Notice = "“购置费”录入有误";
                return;
            }

            if (!decimal.TryParse(this.FuJiaFei.Text, out f2))
            {
                this.Notice = "“材料及施工费”录入有误";
                return;
            }
            this.ChengBen.Text = (f1 + f2).ToString();
            int billid=0;
            int.TryParse(this.BillID.Text, out billid);
            if (billid > 0)
            {
                ParameterCollection pc = this.SqlDataSource1.UpdateParameters;
                pc["BillID"].DefaultValue = billid.ToString();
                pc["编码"].DefaultValue = this.BianHao.Text;
                pc["名称"].DefaultValue = this.MingCheng.Text;
                pc["投产日期"].DefaultValue = d1.ToShortDateString();
                pc["型号"].DefaultValue = this.XingHao.Text;
                pc["厂家"].DefaultValue = this.ChangJia.Text;
                pc["出厂编号"].DefaultValue = this.ChuChangBH.Text;
                pc["出厂日期"].DefaultValue = this.Date2.Text; 
                pc["大小"].DefaultValue = this.DaXiao.Text;
                pc["功率"].DefaultValue = this.GongLv.Text;
                pc["安装地点"].DefaultValue = this.DiDian.Text;
                pc["折旧年限"].DefaultValue = this.NianXian.Text;
                pc["购置费"].DefaultValue = f1.ToString();
                pc["附加费"].DefaultValue = f2.ToString();
                pc["验收意见"].DefaultValue = this.YanShouYiJian.Text;
                pc["验收人"].DefaultValue = this.YanShouRen.Text;
                pc["备注"].DefaultValue = this.BeiZhu.Text;
                pc["资产编码"].DefaultValue = this.ZiChan.Text;
                pc["序号"].DefaultValue = this.XuHao.Text;
                if (this.SqlDataSource1.Update()==0)
                {
                    this.Notice = "编号重复，保存失败";
                    return;
                }
            }
            else
            {
                ParameterCollection pc = this.SqlDataSource1.InsertParameters;
                pc["编码"].DefaultValue = this.BianHao.Text;
                pc["名称"].DefaultValue = this.MingCheng.Text;
                pc["投产日期"].DefaultValue = d1.ToShortDateString();
                pc["型号"].DefaultValue = this.XingHao.Text;
                pc["厂家"].DefaultValue = this.ChangJia.Text;
                pc["出厂编号"].DefaultValue = this.ChuChangBH.Text;
                pc["出厂日期"].DefaultValue = this.Date2.Text; 
                pc["大小"].DefaultValue = this.DaXiao.Text;
                pc["功率"].DefaultValue = this.GongLv.Text;
                pc["安装地点"].DefaultValue = this.DiDian.Text;
                pc["折旧年限"].DefaultValue = this.NianXian.Text;
                pc["购置费"].DefaultValue = f1.ToString();
                pc["附加费"].DefaultValue = f2.ToString();
                pc["验收意见"].DefaultValue = this.YanShouYiJian.Text;
                pc["验收人"].DefaultValue = this.YanShouRen.Text;
                pc["备注"].DefaultValue = this.BeiZhu.Text;
                pc["单位编码"].DefaultValue = this.BuMeng.Text;
                pc["资产编码"].DefaultValue = this.ZiChan.Text;
                pc["序号"].DefaultValue = this.XuHao.Text;
                this.SqlDataSource1.Insert();
                if (this.BillID.Text == "0")
                {
                    this.Notice = "编号重复，保存失败";
                    return;
                }

            }
            this.Notice = "保存成功";
            
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
           this.BillID.Text = ((int)e.Command.Parameters[0].Value).ToString();
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (e.Command.Parameters[1].Value==DBNull.Value)
            {
                return;
            }
            this.BillID.Text = ((int)e.Command.Parameters[0].Value).ToString();
            this.BianHao.Text = (string)e.Command.Parameters[1].Value;
            this.MingCheng.Text = (string)e.Command.Parameters[2].Value;
            if (e.Command.Parameters[3].Value != DBNull.Value)
            {
                this.Date1.Text = ((DateTime)e.Command.Parameters[3].Value).ToLongDateString();
            }
            this.XingHao.Text = (string)e.Command.Parameters[4].Value;
            this.ChangJia.Text = (string)e.Command.Parameters[5].Value;
            this.ChuChangBH.Text = (string)e.Command.Parameters[6].Value;
            if (e.Command.Parameters[7].Value != DBNull.Value)
            {
                this.Date2.Text = (string)e.Command.Parameters[7].Value;
            }
            this.DaXiao.Text = (string)e.Command.Parameters[8].Value;
            if (e.Command.Parameters[9].Value != DBNull.Value)
            {
                this.GongLv.Text = ((double)e.Command.Parameters[9].Value).ToString();
            }
            this.DiDian.Text = (string)e.Command.Parameters[10].Value;
            if (e.Command.Parameters[11].Value != DBNull.Value)
            {
                this.NianXian.Text = ((short)e.Command.Parameters[11].Value).ToString();
            }
            if (e.Command.Parameters[12].Value != DBNull.Value)
            {
                this.GouZhiFei.Text = ((decimal)e.Command.Parameters[12].Value).ToString("0.00");
            }
            if (e.Command.Parameters[13].Value != DBNull.Value)
            {
                this.FuJiaFei.Text = ((decimal)e.Command.Parameters[13].Value).ToString("0.00");
            }

            if (e.Command.Parameters[12].Value != DBNull.Value && e.Command.Parameters[13].Value != DBNull.Value)
            {
                this.ChengBen.Text = (((decimal)e.Command.Parameters[12].Value) + ((decimal)e.Command.Parameters[13].Value)).ToString(".00");
            }

            this.YanShouYiJian.Text = (string)e.Command.Parameters[14].Value;
            this.YanShouRen.Text = (string)e.Command.Parameters[15].Value;
            this.BeiZhu.Text = (string)e.Command.Parameters[16].Value;
            this.BuMeng.Text = ((int)e.Command.Parameters[17].Value).ToString("D6");
            this.ZiChan.Text = ((int)e.Command.Parameters[18].Value).ToString("D8");
            this.XuHao.Text = ((short)e.Command.Parameters[19].Value).ToString("D4");
            this.BuMeng.Enabled = false;
        }

        protected void BuMeng_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetBianMa();
        }

        protected void ZiChan_TextChanged(object sender, EventArgs e)
        {
            this.Notice = "";
            int zc = 0;
            int.TryParse(this.ZiChan.Text, out zc);
            this.SqlDataSource3.SelectParameters["ID"].DefaultValue = zc.ToString();
            this.SqlDataSource3.Select(DataSourceSelectArguments.Empty);
            if (this.Notice != "")
            {
                return;
            }
            SetBianMa();
        }

        protected void XuHao_TextChanged(object sender, EventArgs e)
        {
            SetBianMa();
        }

        private void SetBianMa()
        {
            this.BianHao.Text = "";
            int bm;
            int zc=0;
            int xh=0;
            bm = int.Parse(this.BuMeng.Text);
            int.TryParse(this.ZiChan.Text, out zc);
            int.TryParse(this.XuHao.Text, out xh);
            if (zc < 0 || zc > 99999999)
            {
                this.Notice="资产类别编码录入有误";
                return;
            }
            if (xh < 0 || xh > 9999)
            {
                this.Notice="序号录入有误";
                return;
            }
            this.BianHao.Text = bm.ToString("D6") + zc.ToString("D8")+xh.ToString("D4");
        }

        protected void SqlDataSource3_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (e.Command.Parameters[1].Value == DBNull.Value)
            {
                this.Notice = "资产类别编码不存在";
                return;
            }
            else
            {
                this.NianXian.Text = ((byte)e.Command.Parameters[1].Value).ToString();
            }
        }
    }
}
